<?php

    class Login extends CI_Controller
    {
        
        function __construct()
        {
    	    parent::__construct();
               $this->load->model('user');
       	}
        
        function validate_login()
        {
        	$this->form_validation->set_rules('username', 'Username', 'trim|required|callback_checkfield');
        	$this->form_validation->set_rules('password', 'Password', 'trim|required');

        	if($this->form_validation->run())
        	{
        		$data = array();  
        		$data = $this->user->credentials();
        		$data['user_data'] = $this->user->credentials();

                foreach ($data['user_data'] as $row) 
                {
                     $user_session = array(
                                    'user_id'  => $row -> user_id,
                                    'username' => $row -> username,
                                    'fname'    => $row -> fname, 
                                    'lname'    => $row -> lname
                                    );
                }

                $this->session->set_userdata($user_session);
                redirect('durak/main', 'refresh'); 
        	}
            else
            {
                $data['content'] = 'login';
                $this->load->view('include/template' ,$data);  
            }
        }


        function checkfield($username)
        {    
            $result = $this->user->credentials();   
            if($result!= NULL)
            {
                return TRUE;
            }
            else
            {
                $this->form_validation->set_message('checkfield', 'Invalid Username or Password.');
                return FALSE;
            }
        }
      
    }

?>